package com.htn.common.core.base;

import com.htn.common.core.constant.CommonConstants;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

/**
 * @author guoqianyou
 * @date 2018-05-16 16:57
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
public class DataResponse<T> extends BaseResponse {
	private T data;

	/**
	 * 该构造器仅供全局异常处理使用
	 *
	 * @param status
	 */
	public DataResponse(Exception e, CommonConstants.ResponseStatus status) {
		super(status);
	}

	public static <T> DataResponse<T> success(T result) {
		DataResponse<T> dataResponse = new DataResponse<>();
		dataResponse.setStatus(CommonConstants.ResponseStatus.SUCCESS);
		dataResponse.setCode(CommonConstants.ResponseStatus.SUCCESS.getCode());
		dataResponse.setMessage(CommonConstants.ResponseStatus.SUCCESS.getMessage());
		dataResponse.setData(result);
		return dataResponse;
	}

	public static <T> DataResponse<T> failure(Integer code, String message) {
		DataResponse<T> dataResponse = new DataResponse<>();
		dataResponse.setStatus(CommonConstants.ResponseStatus.FAIL);
		dataResponse.setCode(code);
		dataResponse.setMessage(message);
		return dataResponse;
	}


	/**
	 * 常用的
	 *
	 * @param data
	 */
	public DataResponse(T data) {
		super();
		this.data = data;
	}

	public static <T> DataResponse<T> failure(CommonConstants.ResponseStatus status) {
		return failure(status.getCode(), status.getMessage());
	}

	/**
	 * @param status
	 * @param data
	 */
	public DataResponse(CommonConstants.ResponseStatus status, T data) {
		super(status);
		this.data = data;
	}


}
